#!/usr/bin/python2.7
#
# Copyright 2011 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from listen.core import SnippetPattern, UtterancePattern
from listen.environments import eclipse
from third_party.active_state.recipes.autoassign import autoassign

'''
Created on Dec 9, 2011

@author: ibrahimelfar
'''

class TagInfo:
    @autoassign
    def __init__(self, name, mode, spoken = '', required_attributes = [], start = '', end = '', attributes = '', html5 = True): pass

tags = [
TagInfo('!DOCTYPE', 'm', spoken = '', required_attributes = [], start = '!DOCTYPE html', end = ' '),
TagInfo('a', 's', spoken = 'hyperlink', required_attributes = ['href']),
TagInfo('abbr', 's', spoken = 'abbreviation', required_attributes = []),
TagInfo('acronym', 's', spoken = '', required_attributes = [], html5 = False),
TagInfo('address', 'm', spoken = '', required_attributes = []),
TagInfo('applet', 'm', spoken = '', required_attributes = [], html5 = False),
TagInfo('area', 's', spoken = '', required_attributes = []),
TagInfo('article', 'm', spoken = '', required_attributes = []),
TagInfo('aside', 'm', spoken = '', required_attributes = []),
TagInfo('audio', 'm', spoken = '', required_attributes = ['src', 'type']),
TagInfo('b', 's', spoken = 'bold', required_attributes = []),
TagInfo('base', 'm', spoken = '', required_attributes = ['href', 'target']),
TagInfo('basefont', 'm', spoken = 'base font', required_attributes = [], html5 = False),
TagInfo('bdi', 's', spoken = 'bidirectional isolation', required_attributes = []),
TagInfo('bdo', 'm', spoken = 'bidirectional override', required_attributes = ['dir']),
TagInfo('big', 's', spoken = '', required_attributes = [], html5 = False),
TagInfo('blockquote', 'm', spoken = 'block quote', required_attributes = ['cite']),
TagInfo('body', 'm', spoken = '', required_attributes = []),
TagInfo('br', 'a', spoken = 'line break', required_attributes = []),
TagInfo('button', 's', spoken = '', required_attributes = ['name', 'type', 'value']),
TagInfo('canvas', 'm', spoken = '', required_attributes = ['id']),
TagInfo('caption', 's', spoken = '', required_attributes = []),
TagInfo('center', 's', spoken = '', required_attributes = [], html5 = False),
TagInfo('cite', 's', spoken = '', required_attributes = []),
TagInfo('code', 's', spoken = '', required_attributes = []),
TagInfo('col', 's', spoken = 'column', required_attributes = ['span']),
TagInfo('colgroup', 'm', spoken = 'column group', required_attributes = ['span']),
TagInfo('command', 's', spoken = '', required_attributes = ['type', 'label', 'onclick']),
TagInfo('datalist', 'm', spoken = 'data list', required_attributes = ['id']),
TagInfo('dd', 'm', spoken = '', required_attributes = []),
TagInfo('del', 's', spoken = 'deleted', required_attributes = []),
TagInfo('details', 'm', spoken = '', required_attributes = []),
TagInfo('dfn', 'm', spoken = '', required_attributes = []),
TagInfo('dir', 'm', spoken = '', required_attributes = []),
TagInfo('div', 'm', spoken = '', required_attributes = []),
TagInfo('dl', 'm', spoken = '', required_attributes = []),
TagInfo('dt', 'm', spoken = '', required_attributes = []),
TagInfo('em', 'm', spoken = '', required_attributes = []),
TagInfo('embed', 'm', spoken = '', required_attributes = []),
TagInfo('fieldset', 'm', spoken = '', required_attributes = []),
TagInfo('figcaption', 'm', spoken = '', required_attributes = []),
TagInfo('figure', 'm', spoken = '', required_attributes = []),
TagInfo('font', 'm', spoken = '', required_attributes = []),
TagInfo('footer', 'm', spoken = '', required_attributes = []),
TagInfo('form', 'm', spoken = '', required_attributes = []),
TagInfo('frame', 'm', spoken = '', required_attributes = []),
TagInfo('frameset', 'm', spoken = '', required_attributes = []),
TagInfo('h1', 'm', spoken = '', required_attributes = []),
TagInfo('h2', 'm', spoken = '', required_attributes = []),
TagInfo('h3', 'm', spoken = '', required_attributes = []),
TagInfo('h4', 'm', spoken = '', required_attributes = []),
TagInfo('h5', 'm', spoken = '', required_attributes = []),
TagInfo('h6', 'm', spoken = '', required_attributes = []),
TagInfo('head', 'm', spoken = '', required_attributes = []),
TagInfo('header', 'm', spoken = '', required_attributes = []),
TagInfo('hgroup', 'm', spoken = '', required_attributes = []),
TagInfo('hr', 'm', spoken = '', required_attributes = []),
TagInfo('html', 'm', spoken = '', required_attributes = []),
TagInfo('i', 'm', spoken = '', required_attributes = []),
TagInfo('iframe', 'm', spoken = '', required_attributes = []),
TagInfo('img', 'm', spoken = '', required_attributes = []),
TagInfo('input', 'm', spoken = '', required_attributes = []),
TagInfo('ins', 'm', spoken = '', required_attributes = []),
TagInfo('keygen', 'm', spoken = '', required_attributes = []),
TagInfo('kbd', 'm', spoken = '', required_attributes = []),
TagInfo('label', 'm', spoken = '', required_attributes = []),
TagInfo('legend', 'm', spoken = '', required_attributes = []),
TagInfo('li', 'm', spoken = '', required_attributes = []),
TagInfo('link', 'm', spoken = '', required_attributes = []),
TagInfo('map', 'm', spoken = '', required_attributes = []),
TagInfo('mark', 'm', spoken = '', required_attributes = []),
TagInfo('menu', 'm', spoken = '', required_attributes = []),
TagInfo('meta', 'm', spoken = '', required_attributes = []),
TagInfo('meter', 'm', spoken = '', required_attributes = []),
TagInfo('nav', 'm', spoken = '', required_attributes = []),
TagInfo('noframes', 'm', spoken = '', required_attributes = []),
TagInfo('noscript', 'm', spoken = '', required_attributes = []),
TagInfo('object', 'm', spoken = '', required_attributes = []),
TagInfo('ol', 'm', spoken = '', required_attributes = []),
TagInfo('optgroup', 'm', spoken = '', required_attributes = []),
TagInfo('option', 'm', spoken = '', required_attributes = []),
TagInfo('output', 'm', spoken = '', required_attributes = []),
TagInfo('p', 'm', spoken = '', required_attributes = []),
TagInfo('param', 'm', spoken = '', required_attributes = []),
TagInfo('pre', 'm', spoken = '', required_attributes = []),
TagInfo('progress', 'm', spoken = '', required_attributes = []),
TagInfo('q', 'm', spoken = '', required_attributes = []),
TagInfo('rp', 'm', spoken = '', required_attributes = []),
TagInfo('rt', 'm', spoken = '', required_attributes = []),
TagInfo('ruby', 'm', spoken = '', required_attributes = []),
TagInfo('s', 'm', spoken = '', required_attributes = []),
TagInfo('samp', 'm', spoken = '', required_attributes = []),
TagInfo('script', 'm', spoken = '', required_attributes = []),
TagInfo('section', 'm', spoken = '', required_attributes = []),
TagInfo('select', 'm', spoken = '', required_attributes = []),
TagInfo('small', 'm', spoken = '', required_attributes = []),
TagInfo('source', 'm', spoken = '', required_attributes = []),
TagInfo('span', 'm', spoken = '', required_attributes = []),
TagInfo('strike', 'm', spoken = '', required_attributes = []),
TagInfo('strong', 'm', spoken = '', required_attributes = []),
TagInfo('style', 'm', spoken = '', required_attributes = []),
TagInfo('sub', 'm', spoken = '', required_attributes = []),
TagInfo('summary', 'm', spoken = '', required_attributes = []),
TagInfo('sup', 'm', spoken = '', required_attributes = []),
TagInfo('table', 'm', spoken = '', required_attributes = []),
TagInfo('tbody', 'm', spoken = '', required_attributes = []),
TagInfo('td', 'm', spoken = '', required_attributes = []),
TagInfo('textarea', 'm', spoken = '', required_attributes = []),
TagInfo('tfoot', 'm', spoken = '', required_attributes = []),
TagInfo('th', 'm', spoken = '', required_attributes = []),
TagInfo('thead', 'm', spoken = '', required_attributes = []),
TagInfo('time', 'm', spoken = '', required_attributes = []),
TagInfo('title', 'm', spoken = '', required_attributes = []),
TagInfo('tr', 'm', spoken = '', required_attributes = []),
TagInfo('track', 'm', spoken = '', required_attributes = []),
TagInfo('tt', 'm', spoken = '', required_attributes = []),
TagInfo('u', 'm', spoken = '', required_attributes = []),
TagInfo('ul', 'm', spoken = '', required_attributes = []),
TagInfo('var', 'm', spoken = '', required_attributes = []),
TagInfo('video', 'm', spoken = '', required_attributes = []),
TagInfo('wbr', 'm', spoken = '', required_attributes = []),
]

#all_tags = ['!DOCTYPE',
#            'a',
#            'abbr',
#            'acronym',
#            'address',
#            'applet',
#            'area',
#            'article',
#            'aside',
#            'b',
#            'base',
#            'basefont',
#            'bdo',
#            'big',
#            'blockquote',
#            'body',
#            'br',
#            'button',
#            'canvas',
#            'caption',
#            'center',
#            'cite',
#            'code',
#            'col',
#            'colgroup',
#            'command',
#            'datalist',
#            'dd',
#            'del',
#            'details',
#            'dfn',
#            'dir',
#            'div',
#            'dl',
#            'dt',
#            'em',
#            'embed',
#            'fieldset',
#            'figcaption',
#            'font',
#            'form',
#            'frame',
#            'frameset',
#            'head',
#            'h1',
#            'h2',
#            'h3',
#            'h4',
#            'h5',
#            'h6',
#            'hr',
#            'html',
#            'i',
#            'iframe',
#            'img',
#            'input',
#            'ins',
#            'kbd',
#            'label',
#            'legend',
#            'li',
#            'link',
#            'map',
#            'menu',
#            'meta',
#            'noframes',
#            'noscript',
#            'object',
#            'ol',
#            'optgroup',
#            'option',
#            'p',
#            'param',
#            'pre',
#            'q',
#            's',
#            'samp',
#            'script',
#            'select',
#            'small',
#            'span',
#            'strike',
#            'strong',
#            'style',
#            'sub',
#            'sup',
#            'table',
#            'tbody',
#            'td',
#            'textarea',
#            'tfoot',
#            'th',
#            'thead',
#            'title',
#            'tr',
#            'tt',
#            'u',
#            'ul',
#            'var',
#            ]
#
#standalone_tags = [
#            'br',
#            'hr',
#            ]
#
#multiple_line_tags = [
#            'address',
#            'applet',
#            'area',
#            'blockquote',
#            'body',
#            'cite',
#            'code',
#            'col',
#            'colgroup',
#            'dd',
#            'dir',
#            'div',
#            'dl',
#            'dt',
#            'fieldset',
#            'form',
#            'frame',
#            'frameset',
#            'head',
#            'html',
#            'iframe',
#            'legend',
#            'map',
#            'menu',
#            'ol',
#            'optgroup',
#            'p',
#            'pre',
#            'samp',
#            'script',
#            'span',
#            'style',
#            'table',
#            'tbody',
#            'tfoot',
#            'th',
#            'thead',
#            'tr',
#            'tt',
#                      ]
#
#
#spoken_form = dict([(x, x) for x in all_tags])
#spoken_form.update(
#{'!DOCTYPE': 'document type',
#            'a': 'hyperlink',
#            'abbr': 'abbreviation',
#            'b': 'bold',
#            'basefont': 'base font',
#            'bdo': 'bidirectinal override',
#            'blockquote': 'block quote',
#            'br': 'line break',
#            'col': 'column',
#            'colgroup': 'column group',
#            'datalist': 'data list',
#            'dd': 'data definition',
#            'del': 'deleted',
#            'dfn': 'definition',
#            'dir': 'directory',
#            'div': 'division',
#            'dl': 'definition list',
#            'dt': 'data type',
#            'em': 'emphasis',
#            'fieldset': 'field set',
#            'frameset': 'frame set',
#            'h1': 'heading one',
#            'h2': 'heading two',
#            'h3': 'heading three',
#            'h4': 'heading four',
#            'h5': 'heading five',
#            'h6': 'heading six',
#            'hr': 'horizontal rule',
#            'html': 'HTML',
#            'i': 'italic',
#            'iframe': 'I frame',
#            'img': 'image',
#            'ins': 'deleted',
#            'kbd': 'keyboard',
#            'li': 'list item',
#            'noframes': 'no frames',
#            'noscript': 'no script',
#            'ol': 'ordered list',
#            'optgroup': 'option group',
#            'p': 'paragraph',
#            'param': 'parameter',
#            'pre': 'preformatted',
#            'q': 'short quotation',
#            's': 'strikethrough',
#            'samp': 'sample',
#            'sub': 'subscript',
#            'sup': 'superscript',
#            'tbody': 'table body',
#            'td': 'table cell',
#            'textarea': 'text area',
#            'tfoot': 'table footer',
#            'th': 'table header',
#            'tr': 'table row',
#            'tt': 'teletype',
#            'u': 'underline',
#            'ul': 'unordered list',
#            'var': 'variable',
#            })

def tostring(attributes):
    converts = []
    for attribute in attributes:
        converts += ' {0} = "${{{0}}}"'.format(attribute) 
    return ''.join(converts)

def preprocess():
    for tag in tags:
        if tag.spoken == '':
            tag.spoken = tag.name
        if tag.start == '':
            tag.start = tag.name
        if tag.end == '':
            tag.end = tag.name
        if len(tag.required_attributes) > 0:
            tag.attributes = tostring(tag.required_attributes)

preprocess()
multiple_line_tags = [tag for tag in tags if tag.mode == 'm']
single_line_tags = [tag for tag in tags if tag.mode == 's']
standalone_tags = [tag for tag in tags if tag.mode == 'a']
spoken_tags = [tag.spoken for tag in tags]
create_new=['create', 'new', 'add', 'insert']
mark_that=['mark', 'mark as', 'mark that as', 'surround with', 'surround']

vocabulary = {}
vocabulary['en-us'] = dict(
                           create_tag = UtterancePattern(['[create_new] {{tag.spoken}}'],
                                                    'create-{{tag.spoken}}',
                                                    tag = tags),
                           tag_that = UtterancePattern(['[mark_that] {{tag.spoken}}'],
                                                  'create-{{tag.spoken}}',
                                                  tag = tags),
                           )

utterance_variables = [create_new, mark_that, spoken_tags]
utterance_to_snippet_mapping = dict(
                               create_tag = 'create-{{1}}',
                               tag_that = 'mark-{{1}}'
                               )

patterns = dict(
                create_tag = [
                              SnippetPattern('create-{{tag.spoken}}',
                                              '<{{tag.start}}{{tag.attributes}}>${cursor}</{{tag.end}}>',
                                              tag = single_line_tags),
                              SnippetPattern('create-{{tag.spoken}}',
                                              '<{{tag.start}}{{tag.attributes}}>{{break}}${cursor}{{break}}</{{tag.end}}>',
                                              tag = multiple_line_tags),
                              SnippetPattern('create-{{tag.spoken}}',
                                              '<{{tag.name}}{{tag.attributes}}/>${cursor}',
                                              tag = standalone_tags),
                              SnippetPattern('create-tag',
                                              '<${tag}>${cursor}</${tag}>')
                              ],

                tag_that = [
                              SnippetPattern('mark-{{tag.spoken}}',
                                              '<{{tag.start}}{{tag.attributes}}>${cursor}</{{tag.end}}>',
                                              tag = single_line_tags),
                              SnippetPattern('mark-{{tag.spoken}}',
                                              '<{{tag.start}}{{tag.attributes}}>{{break}}${cursor}{{break}}</{{tag.end}}>',
                                              tag = multiple_line_tags),
                              SnippetPattern('mark-{{tag.spoken}}',
                                              '<{{tag.name}}{{tag.attributes}}/>${cursor}',
                                              tag = standalone_tags),
                              SnippetPattern('mark-tag',
                                              '<${tag}>${cursor}</${tag}>'),
                              ],
                )

default_eclipse_contexts = [eclipse.Context(name = 'html_all', language = 'html')]
eclipse_contexts = dict(
                        create_tag = default_eclipse_contexts,
                        tag_that = default_eclipse_contexts,
                        )

naturallyspeaking_contexts = dict(
                                  create_tag = default_eclipse_contexts,
                                  tag_that = default_eclipse_contexts,
                                  )
